﻿#include "servermonitorcontroller.h"

#include <cpuuse.h>
#include <diskuse.h>
#include <memoryinfo.h>
#include <responesutils.h>
#include <serverinfo.h>
#include <servermonitorcenter.h>


ServerMonitorController::ServerMonitorController(QObject *parent) : HttpRequestHandler(parent)
{

}

ServerMonitorController::~ServerMonitorController()
{

}

void ServerMonitorController::service(HttpRequest &request, HttpResponse &response)
{
    QString path = request.getPath();
    QStringList pathList = path.split("/");
    if (pathList.size() < 3)
    {
        this->returnError(response);
        return;
    }
    QString method = pathList.value(3);
    QByteArray responseMsg;
    QString methodType = request.getMethod();

    qDebug() << __FUNCTION__ << methodType;

    if (methodType == "GET")
    {
        if (method == "serverinfo")
        {
            return getServerInfo(request, response);
        }
        if (method == "cpuinfolist")
        {
            return getCpuInfoList(request, response);
        }
        if (method == "memoryinfolist")
        {
            return getMemoryInfoList(request, response);
        }
        if (method == "diskinfolist")
        {
            return getDiskInfoList(request, response);
        }
        if (method == "cpuuselist")
        {
            return getCupUseList(request, response);
        }
        if (method == "memoryuselist")
        {
            return getMemoryUseList(request, response);
        }
        if (method == "virtualmemoryuselist")
        {
            return getVirtualMemoryUseList(request, response);
        }
        if (method == "diskuselist")
        {
            return getDiskUseList(request, response);
        }
    }
    this->returnError(response);
}

void ServerMonitorController::getServerInfo(HttpRequest &request, HttpResponse &response)
{
    ServerInfo info;
    ServerMonitorCenter::getInstance().getServerInfo(info);
    return ResponesUtils::resultResponse(response,info.toJsonObject());
}

void ServerMonitorController::getCpuInfoList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<CpuInfo> list;
    ServerMonitorCenter::getInstance().getCpuInfoList(list);
    for(CpuInfo &info:list){
        jsonArray.append(info.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void ServerMonitorController::getMemoryInfoList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<MemoryInfo> list;
    ServerMonitorCenter::getInstance().getMemoryInfoList(list);
    for(MemoryInfo &info:list){
        jsonArray.append(info.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void ServerMonitorController::getDiskInfoList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<DiskInfo> list;
    ServerMonitorCenter::getInstance().getDiskInfoList(list);
    for(DiskInfo &info:list){
        jsonArray.append(info.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void ServerMonitorController::getCupUseList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<CpuUse> list;
    ServerMonitorCenter::getInstance().getCpuUseList(list);
    for(CpuUse &use:list){
        jsonArray.append(use.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void ServerMonitorController::getMemoryUseList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<MemoryUse> list;
    ServerMonitorCenter::getInstance().getMemoryUseList(list);
    for(MemoryUse &use:list){
        jsonArray.append(use.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void ServerMonitorController::getVirtualMemoryUseList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<MemoryUse> list;
    ServerMonitorCenter::getInstance().getVirtualMemoryUseList(list);
    for(MemoryUse &use:list){
        jsonArray.append(use.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void ServerMonitorController::getDiskUseList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<DiskUse> list;
    ServerMonitorCenter::getInstance().getDiskUseList(list);
    for(DiskUse &use:list){
        jsonArray.append(use.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}
